﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Web;
using System.Web.Script.Serialization;
namespace LanguageInfo.BLL
{
    public class Common
    {
        #region 将datatable转换成json格式字符串
        /// <summary>
        /// 将datatable转换成json格式字符串
        /// </summary>
        /// <param name="tmpdt">源数据</param>
        /// <param name="sortName">排序字段</param>
        /// <param name="sortOrder">排序方式</param>
        /// <param name="pageNo">当前页</param>
        /// <param name="pageNum">页大小</param>
        /// <returns></returns>
        public static string ConvertDataToJson(DataTable tmpdt, string sortName, string sortOrder, int pageNo, int pageNum)
        {
            FlexigridObject fo = new FlexigridObject();

            DataView dv = tmpdt.DefaultView;
            dv.Sort = string.Format("{0} {1}", sortName, sortOrder);
            DataTable ds = dv.ToTable();
            fo.page = pageNo;
            fo.total = ds.Rows.Count;

            for (int i = (pageNo - 1) * pageNum; i < pageNum * pageNo && i < fo.total; i++)
            {
                List<string> row = new List<string>();
                for (int col = 0; col < ds.Columns.Count; col++)
                {
                    row.Add(ds.Rows[i][col].ToString());

                }
                FlexigridRow fr = new FlexigridRow() { id = ds.Rows[i][0].ToString(), cell = row };
                fo.rows.Add(fr);
            }
            JavaScriptSerializer js = new JavaScriptSerializer();
            string outStr = js.Serialize(fo);//转换成json对象
            return outStr;
        }
        #endregion

        #region 去除转义字符
        public static string ProcessString(string src)
        {
            src = src.Replace("\r\n", "<br />").Replace("\"", "").Replace("\r", "<br />").Replace("\n", "<br />").Replace("\t", "&nbsp; &nbsp; ");
            return src;
        }

        #endregion

        #region 忽略大小写替换
        public static string ReplaceIgnoreCase(string src, string oldvalue, string newvalue = null)
        {
            string str = src.ToLower();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < oldvalue.Length; i++)
            {
                sb.Append("@");
            }
            str = str.Replace(oldvalue.ToLower(), sb.ToString());
            int startIndex = str.IndexOf('@');
            int endIndex = str.LastIndexOf('@');
            string srcvalue = src.Substring(startIndex, endIndex - startIndex + 1);
            if (newvalue != null)
            {
                return src.Replace(srcvalue, newvalue);
            }
            return srcvalue;
        }
        #endregion

    }
}
